

namespace Adoor.Object.Relational
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Data;
    using System.Diagnostics;
    using Adoor.Expression;
    using Adoor.Object;
    using Adoor.Object.Entity;
    using Adoor.Relational;
    using Adoor.Data;  // For SmartKey

    public class SmartKeyMapping<TEntity> : OidData<TEntity>
    {
        public SmartKeyMapping(Adoor.Object.Domain.EntityData entityData, XmlNode metadata)
            : base(entityData, metadata)
        {
            if (this.DataColumns.Length != 1)
                throw new ArgumentException("columns.Length != -1", "key");
        }

        public override string GetOid(KeyValue key)
        {
            if (key.Values.Length != 1)
                throw new ArgumentException("key.Length != -1", "key");
            //return Convert.ToInt32(key.Values[0]).ToString("X8");			
            if (DBNull.Value == key.Values[0])
                return null;

            return key.Values[0].ToString();
        }

        public override KeyValue ExtractKey(string oid)
        {
            try
            {
                return new KeyValue(int.Parse(oid));
            }
            catch (OverflowException)
            {
                throw new Exception("invalid key format");
            }
        }

        //		public override EntityData GetActualEntityData2(string oid)
        //		{
        //			return this.EntityData.GetActualEntityData(oid);
        //		}
    }

  
}
